//
//  FansInfoViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FansInfoViewController.h"
#import "EditUserInfoViewController.h"
#import "UserIconViewController.h"
#import "FansFocusCell.h"
#import "InfoIconCell.h"

@interface FansInfoViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSDictionary *_userInfoDict;
    NSArray *_titleArray;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UserModel *userModel;
@property (nonatomic, copy) NSString *nickname;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *region;
@property (nonatomic, copy) NSString *age;
@property (nonatomic, copy) NSString *file;
@property (nonatomic, copy) NSString *path;
@property (nonatomic, strong) UIImage *iconImage;
@end

@implementation FansInfoViewController

#pragma mark -- 保存用户信息
- (void)SaveUserInfoRequestWithDict:(NSDictionary *)UserDict {
    NSDictionary *dict = [NSDictionary dictionaryWithDictionary:UserDict];
    NSString *urlString = [TEST_URL stringByAppendingString:kSaveUserInfo];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userModel = [[LSUserInfoManager sharedManager] currentUserInfo];
    self.file = @"";
    self.city = @"";
    self.province = @"";
    [self setUpUI];
    [self setNav];
    
    // Do any additional setup after loading the view.
}
- (void)setNav {
    self.navigationItem.title = @"个人资料";
}


#pragma mark -- UI
- (void)setUpUI {
    
    [self creatTableView];
    _titleArray = @[@"头像",@"昵称",@"性别",@"年龄",@"地址"];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        InfoIconCell *cell = [InfoIconCell loadNibCellWithTabelView:tableView];
        cell.titleLabel.text = _titleArray[indexPath.row];
        if (!self.iconImage) {
           [cell.imgView sd_setImageWithURL:[NSURL URLWithString:self.userModel.u_avatar] placeholderImage:nil];
        } else {
            cell.imgView.image = self.iconImage;
        }
        return cell;
    }
    FansFocusCell *cell = [FansFocusCell loadNibCellWithTabelView:tableView];
    cell.detail.font = UIFont(15);
    cell.itemtitle.text = _titleArray[indexPath.row];
    switch (indexPath.row) {
        case 1:
            cell.detail.text = self.userModel.u_nickname.length ? self.userModel.u_nickname : @"未填写";
            self.nickname = self.userModel.u_nickname.length ? self.userModel.u_nickname : @"";
            break;
        case 2:
            if (KString(self.userModel.u_sex).length) {
                cell.detail.text = [self.userModel.u_sex intValue] ? @"男" : @"女";
                self.sex = [self.userModel.u_sex intValue] ? @"1" : @"0";
            } else {
                cell.detail.text = @"未填写";
                self.sex = @"";
            }
            break;
        case 3:
            cell.detail.text = self.userModel.u_age ? KString(self.userModel.u_age) : @"未填写";
            self.age = self.userModel.u_age ? KString(self.userModel.u_age) : @"";
            break;
        case 4:
            cell.detail.text = self.userModel.u_region.length ? self.userModel.u_region : @"未填写";
            self.region = self.userModel.u_region.length ? self.userModel.u_region : @"";
            break;
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:{//头像
            UserIconViewController *userIconVc = [[UserIconViewController alloc] init];

            userIconVc.iconSurce = ^(NSString *imageFile,NSString *imagePath,UIImage *image){
                self.file = imageFile;
                self.path = imagePath;
                self.iconImage = image;
                NSDictionary *dict = @{@"udid":User_ID,
                                       @"token":TOKEN,
                                       @"file":self.file,
                                       @"path":self.path
                                       };
                [self SaveUserInfoRequestWithDict:dict];
            };
            [self.navigationController pushViewController:userIconVc animated:YES];
        }
            break;
        case 1:{//昵称
            [self editActionWithTitle:@"编辑昵称" EditType:EditTypeName IndexPath:indexPath];
        }
            break;
        case 2:{//性别
            [self selectGenderWithIndexPath:indexPath];
        }
            break;
        case 3:{//年龄
            [self editActionWithTitle:@"编辑年龄" EditType:EditTypeAge IndexPath:indexPath];
        }
            break;
        case 4:{//地址
            [self editActionWithTitle:@"编辑地址" EditType:EditTypeAddress IndexPath:indexPath];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -- 区头视图
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

#pragma mark -- 性别选择
- (void)selectGenderWithIndexPath:(NSIndexPath *)indexPath {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
        
    }];
    
    UIAlertAction *man = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        FansFocusCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.detail.text = @"男";
        NSDictionary *dict = @{@"udid":User_ID,
                               @"token":TOKEN,
                               @"sex":@"1",
                               };
        [self SaveUserInfoRequestWithDict:dict];
        
        
    }];
    
    UIAlertAction *women = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        FansFocusCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.detail.text = @"女";
        NSDictionary *dict = @{@"udid":User_ID,
                               @"token":TOKEN,
                               @"sex":@"2",
                               };
        [self SaveUserInfoRequestWithDict:dict];
    }];
    [alertVc addAction:man];
    [alertVc addAction:women];
    [alertVc addAction:cancle];
    [self presentViewController:alertVc animated:YES completion:nil];
    
}

#pragma mark -- editAction
- (void)editActionWithTitle:(NSString *)title EditType:(EditType)editType IndexPath:(NSIndexPath *)indexpath {
    
    EditUserInfoViewController *editVC = [[EditUserInfoViewController alloc] init];
    editVC.itemTitle = title;
    editVC.editType = editType;
    editVC.EditBlock = ^(NSString *editInfo,EditType editType){
        FansFocusCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
        cell.detail.text = editInfo;
        switch (editType) {
            case EditTypeName:{
                NSDictionary *dict = @{@"udid":User_ID,
                                       @"token":TOKEN,
                                       @"nickname":editInfo,
                                       };
                [self SaveUserInfoRequestWithDict:dict];
            }
                break;
            case EditTypeAge:{
                NSDictionary *dict = @{@"udid":User_ID,
                                       @"token":TOKEN,
                                       @"age":editInfo,
                                       };
                [self SaveUserInfoRequestWithDict:dict];
            }
                break;
            case EditTypeAddress:{
                NSDictionary *dict = @{@"udid":User_ID,
                                       @"token":TOKEN,
                                       @"province":@"",
                                       @"city":@"",
                                       @"region":editInfo
                                       };
                [self SaveUserInfoRequestWithDict:dict];
            }
                break;

                
            default:
                break;
        }
        
    };
    [self.navigationController pushViewController:editVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
